package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.bussness.domain.RechargeOffline;
import cn.wolfcode.p2p.bussness.query.PlatformBankinfoQueryObject;
import cn.wolfcode.p2p.bussness.service.IPlatformBankInfoService;
import cn.wolfcode.p2p.bussness.service.IRechargOfflineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RechargeOfflineController {

    @Autowired
    private IPlatformBankInfoService platformBankInfoService;

    @Autowired
    private IRechargOfflineService rechargOfflineService;

    @RequestMapping("/recharge")
    public String rechargePage(@ModelAttribute("qo") PlatformBankinfoQueryObject qo, Model model) {
        //banks
        model.addAttribute("banks", platformBankInfoService.selectAll());
        //pageInfo
        model.addAttribute("pageInfo", platformBankInfoService.queryPage(qo));
        return "recharge";
    }

    @RequestMapping("/recharge_save")
    @ResponseBody
    public AjaxResult rechargeSave(RechargeOffline rechargeOffline) {
        AjaxResult result = null;
        try {
            rechargOfflineService.apply(rechargeOffline);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
